import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Arrays;
import java.util.Objects;
import java.util.Scanner;

/**
 * @Description TODO
 * @Date 2022/3/28 20:59
 * @Author 轻歌
 */
public class SearchPuncDSP {
    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        System.out.println("请输入系统变量 v,k1,k2,e,λ :");
        Scanner scanner = new Scanner(System.in);
        String[] numStrs = scanner.nextLine().split("[,;\\s]+");
        int[] nums = new int[5];
        int index = 0;
        for (String numStr :numStrs){
            nums[index++] = Integer.parseInt(numStr);
        }

        PuncDiffSetPair puncDiffSetPair = new PuncDiffSetPair(nums);
        Objects.requireNonNull(puncDiffSetPair,"输入数据不合法");

        System.out.println("searching...");
        File file = new File("result.txt");
        if (!file.exists()) {
            try {
                System.out.println("create file result.txt:" + file.createNewFile());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try (FileWriter writer = new FileWriter(file)) {
            System.out.println("共搜索到"+searchPunc(puncDiffSetPair, writer)+"个满足给定参数的屏蔽差集偶");
        } catch (IOException e) {
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("搜索耗时：" + (endTime - startTime) + "毫秒");
        System.out.println("对应参数的屏蔽差集偶搜索完毕，结果保存在result.txt");

    }

    public static int searchPunc(PuncDiffSetPair puncDiffSetPair, Writer writer) throws IOException {
        boolean[] allIndex = new boolean[puncDiffSetPair.getV()];
        Arrays.fill(allIndex,true);

        int countPunc = 0;
        //todo 根据已知的参数去找集合 先确定 e
        int[][] eSetAllComb = CombinationList.allCombList(puncDiffSetPair.getV(), puncDiffSetPair.getE());

        //todo 根据下标对应的布尔数组去取对应的元素得到集合
        for (int[] eSetComb : eSetAllComb){
            boolean[] eSetIndex = allIndex.clone();
            int[] eSet = ArrayUtil.indexToElement(eSetIndex, eSetComb);
            puncDiffSetPair.seteSet(eSet);

            //todo 得到 eSet 之后去找 U，即 从 v-e 个数里取 k1-e个
            int[][] u_eSetAllComb = CombinationList.allCombList(puncDiffSetPair.getV() - puncDiffSetPair.getE(), puncDiffSetPair.getK1() - puncDiffSetPair.getE());
            for (int[] u_eSetComb:u_eSetAllComb){
                boolean[] u_eSetIndex = eSetIndex.clone();
                //此处得到的集合是 U-E
                int[] u_eSet = ArrayUtil.indexToElement(u_eSetIndex, u_eSetComb);
                int[] uSet = ArrayUtil.contactArray(u_eSet, eSet);
                puncDiffSetPair.setuSet(uSet);

                //todo eSet 和 uSet 都得到了 ，现在去找 P，即从 v-e-k1 个数里 取 k2-e 个
                int[][] p_eSetAllComb = CombinationList.allCombList(puncDiffSetPair.getV() - puncDiffSetPair.getE() - puncDiffSetPair.getK1(), puncDiffSetPair.getK2() - puncDiffSetPair.getE());
                for (int[] p_eSetComb: p_eSetAllComb){
                    boolean[] p_eSetIndex = u_eSetIndex.clone();
                    int[] p_eSet = ArrayUtil.indexToElement(p_eSetIndex, p_eSetComb);
                    int[] pSet = ArrayUtil.contactArray(p_eSet, eSet);
                    puncDiffSetPair.setpSet(pSet);

                    //todo 目前为止，e,U,P 这三个集合都已得到
                    puncDiffSetPair.setU1Set(puncDiffSetPair.getU1Set());
                    puncDiffSetPair.setwSet(puncDiffSetPair.getwSet());
                    puncDiffSetPair.setW1Set(puncDiffSetPair.getW1Set());
                    if (puncDiffSetPair.isPuncDiffSetPair()){
                        countPunc++;
                        System.out.println(puncDiffSetPair);
                        writer.write(puncDiffSetPair + "\n");
                    }

                }

            }
        }


        return countPunc;
    }
}
